#角色model add by Luxh
common_util = require('../services/common_util')
config = require('../config')
Schema = config.Schema
mongoose = config.mongoose

SysRoleSchema = new Schema({
  role_code:
    type:String
    index:true
  role_name:String
  role_desc:String
  status:Number
  parent_id:String
  resources:[{res_id:String}]
})


exports.SysRole = mongoose.model 'sys_role',SysRoleSchema
SysRole = mongoose.model 'sys_role'

exports.add = (req,callback) ->
  sysRole = new SysRole({
    role_code:common_util.trim(req.param("role_code"))
    role_name:req.param("role_name")
    role_desc:req.param("role_desc")
    status:req.param("status")
    parent_id:req.param("parent_id")
  })
  sysRole.save callback


#根据角色编码判断角色是否已存在
exports.isRoleExist = (role_code,callback) ->
  SysRole.count {role_code:role_code},(err,count) ->
    if err
      callback(err)
    else
      callback(null,count)


